/*-------------------<-- Start of Description-->---------------------\ | Convert an sas dataset or an entire library of sas datasets into | | excel sheet or sheets; | |---------------------<-- End of Description-->----------------------| |--------------------------------------------------------------------| |------------<-- Start of Files or Arguments Needed-->---------------| | libname: the library or directory of datasets to be coverted to | | excel sheet; | | infile: the data set name in the format of dataname.sas7bdat; | | indata: the data set name without extension; | | exceldir: output directory to save the converted excel sheets; | |-------------<-- End of Files or Arguments Needed-->----------------| |--------------------------------------------------------------------| |------------------<-- Start of Files Created-->---------------------| | Usage: %excelcr(libname=, infile=, indata=, exceldir=); | | \-------------------<-- End of Files Created-->---------------------*/ %macro excelcr(libname=, infile=, indata=, exceldir=); /*-------------------------------------------------------\ | Copy Right: Duo Zhou; | | Date: 9-28-2001; | | Purpose: Convert SAS dataset to excel sheet; | \-------------------------------------------------------*/ %local num i ndsns dsn filename extension dataname directory; %local ndsns localvname _i_ _j_; %let libname=; %let infile=; %let indata=; %let exceldir=; %let syspbuff=%sysfunc(translate(%quote(%substr(%quote(%trim(%quote(%left(%quote(&syspbuff))))), 2, %eval(%length(%trim(%quote(%left(%quote(&syspbuff)))))-2))), %str(%'), %str(%"))); %let _excelcrrx_=%sysfunc(rxparse($(1))); %let _xexcelcrpos_=0; %let _xexcelcrlen_=0; %let _xexcelcroldstr_=; %let _xexcelcrnewstr_=; %do %while( %sysfunc(rxmatch(&_excelcrrx_, %quote(&syspbuff))) ); %syscall rxsubstr(_excelcrrx_, syspbuff, _xexcelcrpos_, _xexcelcrlen_); %let _xexcelcroldstr_=%quote(%substr(%quote(&syspbuff), &_xexcelcrpos_, &_xexcelcrlen_)); %let _xexcelcrnewstr_=%quote(%sysfunc(translate(%quote(&_xexcelcroldstr_), À, %quote(%(), Á, %quote(%)), ´, %quote(,), ®, %quote( )))); %let syspbuff=%sysfunc(tranwrd(%quote(&syspbuff), %quote(&_xexcelcroldstr_), %quote(&_xexcelcrnewstr_))); %let _xexcelcrpos_=0; %let _xexcelcrlen_=0; %let _xexcelcroldstr_=; %let _xexcelcrnewstr_=; %end; %let _excelcrnewstr_=&syspbuff; %local _xexcelcrvarcnt_ _xexcelcrvar_; %let _xexcelcrvarcnt_=0; %do %while(%length(%qscan(%nrbquote(&_excelcrnewstr_), %eval(&_xexcelcrvarcnt_+1), %nrbquote(,)))); %let _xexcelcrvarcnt_=%eval(&_xexcelcrvarcnt_+1); %let _xexcelcrvar_=%nrbquote(%qscan(%nrbquote(&_excelcrnewstr_), &_xexcelcrvarcnt_, %nrbquote(,))); %let _xexcelcrvar_=%sysfunc(translate(%quote(&_xexcelcrvar_), '(', 'À', ')', 'Á', ',', '´', ' ', '®')); %let _xexcelcrx2_=%trim(%left(%qscan(%quote(&_xexcelcrvar_), 1, %str(=)))); %let _xexcelcrx3_=%substr(%quote(&_xexcelcrvar_), %eval(%index(%quote(&_xexcelcrvar_),%str(=))+1), %eval(%length(&_xexcelcrvar_)-%index(%quote(&_xexcelcrvar_),%str(=)))); %if (not %index(%BQUOTE(%trim(%BQUOTE(%left(%BQUOTE(&_xexcelcrvar_))))), %str(=))) %then %do; %if (%index(%BQUOTE(%trim(%BQUOTE(%left(%BQUOTE(&_xexcelcrx2_))))), %str(%()) eq 1) and (%index(%BQUOTE(%trim(%BQUOTE(%left(%BQUOTE(%sysfunc(reverse(&_xexcelcrx2_))))))), %str(%))) eq 1) %then %let _xexcelcrx3_=%substr(%quote(%trim(%quote(%left(%quote(&_xexcelcrx2_))))), 2, %eval(%length(%trim(%quote(%left(%quote(&_xexcelcrx2_)))))-2)); %if (%quote(&_xexcelcrvarcnt_) = %quote(1)) %then %let libname=&_xexcelcrx3_; %else %if (%quote(&_xexcelcrvarcnt_) = %quote(2)) %then %let infile=&_xexcelcrx3_; %else %if (%quote(&_xexcelcrvarcnt_) = %quote(3)) %then %let indata=&_xexcelcrx3_; %else %if (%quote(&_xexcelcrvarcnt_) = %quote(4)) %then %let exceldir=&_xexcelcrx3_; %end; %else %if (%index(%BQUOTE(%trim(%BQUOTE(%left(%BQUOTE(&_xexcelcrx3_))))), %str(%()) eq 1) and (%index(%BQUOTE(%trim(%BQUOTE(%left(%BQUOTE(%sysfunc(reverse(&_xexcelcrx3_))))))), %str(%))) eq 1) and (%index(%nrbquote(upcase(%nrbquote(%sysfunc(compress(%nrbquote(&_xexcelcrx2_)))))), WHERE=) le 1) %then %let &_xexcelcrx2_=%substr(%quote(%trim(%quote(%left(%quote(&_xexcelcrx3_))))), 2, %eval(%length(%trim(%quote(%left(%quote(&_xexcelcrx3_)))))-2)); %else %let &_xexcelcrx2_=&_xexcelcrx3_; %end; %let libname=%sysfunc(dequote(&libname)); %let dataname =; options noxwait noxsync; x 'Exit'; %if (%length(%trim(%left(&libname))) le 1) %then %do; %let infile=%sysfunc(dequote(&infile)); %let indata=%sysfunc(dequote(&indata)); %if (%quote(&infile) eq) and (%quote(&indata) ne) %then %do; %let infile=&indata; %end; %if (%substr(&infile, %length(&infile), 1) eq %str(\)) or (%substr(&infile, %length(&infile), 1) eq %str(/)) %then %do; libname _templib_ "&infile"; %let libname=&_templib_; %end; %else %if (%substr(&infile, %length(&infile), 1) ne %str(\)) and (%substr(&infile, %length(&infile), 1) eq %str(/)) %then %do; %let pathrc=%sysfunc(filename(pathrf,&infile)); %let psid=%sysfunc(DOPEN(&pathrf)); %if &psid %then %do; %let drc=%sysfunc(DCLOSE(&psid)); libname _templib_ "&infile"; %let libname=_templib_; %end; %else %if (%sysfunc(fileexist(&infile))) or (%sysfunc(fileexist(%trim(%left(&infile)).sas7bdat))) %then %do; %let dataname=%sysfunc(reverse(%scan(%sysfunc(reverse(&infile)), 1, %str(\/)))); %let directory=%substr(&infile, 1, %eval(%length(&infile)-%length(&dataname))); %if (%index(%quote(&dataname), %str(.))) %then %do; %let extension=%sysfunc(reverse(%scan(%sysfunc(reverse(&infile)), 1, %str(.)))); %let dataname=%substr(&dataname, 1, %eval(%eval(%length(&dataname)-%length(&extension))-1)); %end; libname _templib_ "&directory"; %let libname=_templib_; %end; %end; %end; %else %if (%length(%trim(%left(&libname))) gt 1) %then %do; %if (%quote(&infile) eq) and (%quote(&indata) ne) %then %do; %let infile=&indata; %end; %if (%length(%trim(%left(&infile))) ge 1) %then %do; %let dataname=%sysfunc(reverse(%scan(%sysfunc(reverse(&infile)), 1, %str(\/)))); ; %if (%index(%quote(&dataname), %str(.))) %then %do; %let extension=%sysfunc(reverse(%scan(%sysfunc(reverse(&infile)), 1, %str(.)))); %let dataname=%substr(&dataname, 1, %eval(%eval(%length(&dataname)-%length(&extension))-1)); %end; %end; %end; %let exceldir=%sysfunc(dequote(&exceldir)); %if (%length(%trim(%left(&exceldir))) >1) %then %do; %if (%substr(&exceldir, %length(&exceldir), 1) ne %str(\)) %then %do; %let exceldir=&exceldir.\; %end; %end; proc datasets library=&libname memtype=data; contents out=work._temp1(keep=memname engine nobs varnum name sorted sortedby nodupkey noduprec) data=_all_ noprint; run; proc sort data=_temp1; by memname sorted sortedby; run; %if %nobs(_temp1)>0 %then %do; %let datanames=; /*** Cocatenate data set names ***/ proc sql noprint; select distinct memname into :datanames separated by ' ' from _temp1 where name ne ' ' %if (%quote(&dataname) ne) %then %do; and upcase(memname) = %upcase("&dataname") %end;; quit; %if (%quote(&datanames) ne ) %then %do; /*** Create excel files ***/ %let ndsns=%words(&datanames); %put --> Note: Translating &ndsns data sets &datanames into excel sheets.; ods listing close; %do i=1 %to &ndsns; %let filename=; %let dsn&i=%qscan(&datanames, &i, %str( )); %let filename=&&dsn&i...csv; %put --> Note: Writing to the &i.th excel sheet: "&exceldir.&&dsn&i...csv".; ods csv file="&exceldir.&filename"; proc print data=&libname..&&dsn&i label; title "&&dsn&i"; run; ods csv close; %end; ods listing; %end; %else %do; %put Note: Dataset "&dataname" doesn%str(%') exist; %end; proc datasets library=work nolist; delete _temp1; run;quit; %end; %else %put There is no datasets in the library "&libname".; %mend excelcr;